#!/bin/sh
# TESTS
# VARIABLES
# FUNCTIONS
# MAIN 

#-----------------------------------
# TESTS
#-----------------------------------


if ! command -v lynx >/dev/null 2>&1; then
  printf "command lynx is missing. install it\n"
  exit 1
fi 

if [ "$#" -lt 1  ]; then 
	printf "\nEnter program you search for as argument\n" 1>&2
	exit 1 
fi 


# first run 
dragora_repos="/tmp/dragora_repos"  # VARIABLES
if ! [ -d "$dragora_repos" ]; then
	mkdir "$dragora_repos"
fi 


#-----------------------------------
#  VARIABLES
#-----------------------------------
# setting search_terms or options
# as set is used to get the repos,
# this is a very dirty and confusing workaround
# anything but bulletproof
if [ "$#" -eq 1 ]; then 
  case "$1" in 
    -r|--remove) option="$1"; break;; 
    -*) printf "Wrong option. Use -h\n"; exit 1;; 
     *) search_term="$1";; 
  esac 
elif [ "$#" -eq 2 ]; then
  case "$1" in
    -*) option="$1"; search_term="$2";; 
     *) printf "Wrong usage. option searchterm. try -h \n"; exit 1;;
  esac 
fi 
# the other variables
repo_basename=""
found=""
grep_found=""
answer=""

set -- 		"http://gungre.ch/dragora/repo/kelsoo" \
		"http://gungre.ch/dragora/repo/mprodrigues" \
 		"http://gungre.ch/dragora/repo/n4dir" \
 		"http://gungre.ch/dragora/repo/n4dir/perl_modules" \
 		"http://gungre.ch/dragora/repo/frusen/old-stable" \
		"http://gungre.ch/dragora/repo/frusen/stable" \
		"http://gungre.ch/dragora/repo/frusen/unstable" \
                "http://92.19.232.58:82/dragora/repo/tom/"






#----------------------------------
# FUNCTIONS  
#-----------------------------------
dl_pkg () {
printf "\nDownload the package? (yes or no): \n"
        read -r  answer
	case "$answer" in 
		[Yy]|[Yy]es) echo 'mget *.tlz' | lftp "$1" ;;
	esac 

}

crawl_dragora () {
for i in "$@"; do
	repo_basename=$(basename "$i")
	grep_found=$(lynx -dump -nolist "$i" |  awk '{print $1;}' | \
		    tee "$dragora_repos/$repo_basename" |  \
		    grep "$search_term") 
	if [ -n "$grep_found" ]; then 
		found="yes"
		printf "%s/$grep_found\n" "$i"
		dl_pkg "$i/$grep_found" 
	fi 
done 
}

grep_local () {
for i in "$@"; do 
	repo_basename=$(basename "$i")
	grep_found=$(grep  "$search_term" "$dragora_repos"/"$repo_basename")
	if [ -n "$grep_found" ]; then
		found="yes"
		printf "\n%s found in %s\n\n" "$searchterm" "$dragora_repos/$repo_basename"
	fi 
done 
}

info_msg () {
if [ -z "$found" ]; then 
	printf "\nNot found at community repos\n"
	printf "\ntry jul search %s too\n\n" "$search_term"
	exit 1
fi 
}

usage () {
	printf "   
	Usage: crawl_dragora [options]
	 
	-h | --help  
	display help 

	-r | --remove 
	remove %s, start fresh
  
	-f | --force 
	force download from gungre 
  
	-i 
	force grep %s 
" "$dragora_repos" "$dragora_repos"
}


#----------------------------------
# MAIN work   
#-----------------------------------

#while :; do
	case $option in 
		-h|--help) usage; exit 0;;
		-i) grep_local "$@"; exit 0;; 
		-r|--remove) rm -r  "$dragora_repos"; exit 0;;
		-f|--force) crawl_dragora "$@" ; exit 0;;
		-*) usage; exit 1;;  
#		*) break;;  
	esac
#done 





if  find "$dragora_repos"/*   -mtime -1 | grep -q .  ; then 
	printf "\nLast download less than a day ago\n"
	printf "Will grep the local files: %s\n\n" "$dragora_repos"
	grep_local "$@"
	info_msg
else
	crawl_dragora "$@" 
	info_msg 
fi 



exit 0
